
#set 
set Inda ordered ;
set Ind ordered ;
set Indu ordered ;

# Parameters
param A  { Ind }  ;
param Omega { Ind, Indu}  ;
param theta  { Ind }  ; 
param in_mu  { Ind }  ;
param mu  { Ind }  ;
param factor { Ind }  ;
param variab { Ind } ;
param init_p { Ind } ;
param init_lambda { Ind };
param init_lambda1 { Ind };
param init_frB { Ind };
param keynes { Ind };
param chi { Ind }; 
param B { Ind , Indu };
param phi { Ind }; 
param beta_new { Ind };

# ======================
#      Variables:
# ======================

var p {k in Ind}:= init_p[k] >=0;     #
var lambda {k in Ind}:= init_lambda1[k] >=0 ; #
# var cons_share {k in Ind};
var frB {k in Ind} := init_frB[k] ;

# ==========================================
#          Optimization Problem
# ==========================================

maximize trivial: 0;

subject to

price_equation_frB {k in Ind: factor[k]=-1}: p[k]=(mu[k]/in_mu[k])/A[k]*(sum{j in Indu}(frB[j]^(theta[k])*Omega[k,j]*p[j]^(1-theta[k])))^(1/(1-theta[k]));
price_equation {k in Ind: factor[k]=1}: p[k]=(mu[k]/in_mu[k])/A[k]*(sum{j in Indu}(B[k,j]^(theta[k])*Omega[k,j]*p[j]^(1-theta[k])))^(1/(1-theta[k]));
# if factor[k]>0 select all goods

match_beta_new {k in Ind: variab[k]=1}: beta_new[k] = (frB[k]^(theta[member(1,Ind)])*Omega[member(1,Ind),k]*(p[k]/p[member(1,Ind)])^(1-theta[member(1,Ind)])*A[member(1,Ind)]^(theta[member(1,Ind)]-1));
normalize_frB {a in Inda}: 1 = sum{k in Ind}(Omega[member(1,Ind),k]*frB[k]) ;
restriction_frB {k in Ind: variab[k]<1}: frB[k] = 0 ;

# consumption_share {k in Ind: factor[k]=1}: cons_share[k] = (B[member(1,Ind),k]^(theta[member(1,Ind)])*Omega[member(1,Ind),k]*(p[k]/p[member(1,Ind)])^(1-theta[member(1,Ind)])*A[member(1,Ind)]^(theta[member(1,Ind)]-1));

lambda_equation {k in Ind: factor[k]<2}: lambda[k]=lambda[member(1,Ind)]*mu[member(1,Ind)]^(-1)*frB[k]^(theta[member(1,Ind)])*Omega[member(1,Ind),k]*(p[k]/p[member(1,Ind)])^(1-theta[member(1,Ind)])*(mu[member(1,Ind)]/in_mu[member(1,Ind)])^(1-theta[member(1,Ind)])*A[member(1,Ind)]^(theta[member(1,Ind)]-1)+sum{j in Ind: factor[j]>0}(lambda[j]*mu[j]^(-1)*B[j,k]^(theta[j])*Omega[j,k]*(p[k]/p[j])^(1-theta[j])*(mu[j]/in_mu[j])^(1-theta[j])*A[j]^(theta[j]-1));
# if factor[k]<2 select only goods and factors

lambda_equation1 {k in Ind: factor[k]=3}: lambda[k] = sum {j in Ind: factor[j]=0} chi[j]*lambda[j];
lambda_equation2 {k in Ind: factor[k]=2}: lambda[k] = sum {j in Ind: factor[j]=0} (1-chi[j])*lambda[j];


factor_clearing0 {k in Ind: keynes[k]=0}: lambda[k]=p[k]*(p[k]/p[member(1,Ind)])^(phi[k])*init_lambda[k]*A[k];
factor_clearing1 {k in Ind: keynes[k]<0}: (p[k]-1)*(lambda[k]/p[k]-A[k]*init_lambda[k])=0;
factor_clearing2 {k in Ind: keynes[k]<0}: p[k]>=1;
factor_clearing3 {k in Ind: keynes[k]<0}: lambda[k]/p[k]<=A[k]*init_lambda[k];
# if factor[k]=0 select only  factors

Numeraire {k in Ind}: p[member(334,Ind)]=1;

option solver knitroampl;
option knitro_options "feastol=1e-12 maxit=600";
